<?php
Class PaperModel extends Model
{
	public $id;
	public $linespace;
	public $norepeation;
	public $paperheader;
	public $tasks;
	
	public function __construct()
	{
		parent::Model();
		$this->id=-1;
	}
	
	public function DeleteByID($id)
	{
		$this->db->trans_start();
		try 
		{
			$sql="select ID From papers where PaperBasicID=$id";
			$query=$this->db->query($sql);
			foreach ($query->result() as $row)
			{
				$psi=$row->ID;
				$subsql="Delete from questions where PaperID=$psi";
				$this->db->query($subsql);
			}
			$sql="delete from papers where PaperBasicId=$pbi";
			$this->db->query($sql);
			$sql="delete from paperbasicinfo where id=$id";
			$this->db->query($sql);;
		} catch (Exception $e) 
		{
			$this->db->trans_complete();
			return 0;
		}
	
		$this->db->trans_complete();
		return 1;		
	}
	
	private function GetData(&$con)
	{
		$data=array(
		'LineSpace'=>$this->linespace,
		'NoRepeation'=>$this->norepeation,
		'PaperHeader'=>$this->paperheader,
		'Content'=>$con,
		'CreationDate'=>,
		'UserID'=>);
		
		return $data;
	}
	
	private function Read()
	{
		
	}
	
	public function Add()
	{
		$this->read();
				
		$content=TaskModel::GenerateContent($this->tasks);
		$data=$this->GetData($content);
		try {
			$this->db->insert_string('paperbasicinfo',$data);
		} catch (Exception $e) {
			
			return 0;
		}
		
		return 1;
	}
	
	public function Modify()
	{
		$this->read();
		
		$content=TaskModel::GenerateContent($this->tasks);
		$data=$this->GetData($content);
		try {
			$this->db->Update_string('paperbasicinfo',$data);
		} catch (Exception $e) {
			
			return 0;
		}
		
		return 1;
		
	}
	
	public function Get()
	{
		//read to $id
		
		$sql="select ID,LineSpace,NoRepeation,PaperHeader,Content,CreationDate from paperbasicinfo where ID=$id";
		try {
			$query=$this->db->query($sql);
			$paper=$query->row();
			
			$this->id=$paper->ID;
			$this->linespace=$paper->LindeSpace;
			$this->norepeation=$paper->NoRePeation;
			$this->paperheader=$paper->PaperHeader;
			$this->tasks=TaskModel::FillContent($paper->Content);
		} catch (Exception $e) {
			return 0;
		}
		
		//output
		
		return 1;
	}
	
	public function ReadSearchParameters()
	{
	}
	
	public function Search()
	{
		$param=$this->ReadSearchParameters();
		
		if ($param->ID!=-1)
		{
			$id=$param->ID;
			$sql="select ID,PaperHeader,CreationDate from paperbasicinfo where ID=$id";
		}
		else 
		{
			$where="";
			if ($param->DateFrom!='' || $param->DateTo!='')
			{
				$from=$param->DateFrom;
				$to=$param->DateTo;
				if ($from!='') $where=$where."(CreationDate>=$from and ";
				if ($to!='') $where=$where."(CreationDate<=$to and ";
			}	
			else 
				return 0;	
				
			$sql="select ID,PaperHeader,CreationDate from paperbasicinfo ".$where."1=1";
		}
		
		try {
			$query=$this->db->query($sql);
			foreach ($query->result() as $row)
			{
				$res["ID"]=$row->ID;
				$res["Description"]=$row->PaperHeader;
				$res["CreationDate"]=$row->CreationDate;
				//display
			}
		} catch (Exception $e) {
			return 0;
		}

		return 1;
	}
}

Class TaskModel
{
	public $TaskID;
	public $Categories;
	public $Scores;
	
	public function __construct()
	{
		$this->TaskID=-1;
		$this->Categories=null;
		$this->Scores=-1;
	}
	
	public function Serialize()
	{
		$str='';
		foreach ($this->Categories as $cat)
		{
			$str=$str.$cat->Serialize();
		}
		$str=$str.'#';
		
		return $str;
	}
	
	public function Unserialize($str)
	{
		$con=explode(";", $str);
		foreach ($con as $element)
		{
			$cat=new CategoryModel();
			$cat->Unserialize($element);
			array_push($this->Categories,$cat);
		}
	}	
	
	public static function GenerateContent($tasks)
	{
		$str='';
		foreach ($tasks as $task)
		{
			$str=$str.$task->Serialize();
		}
		
		return $str;
	}
	
	public static function FillContent($str)
	{
		$con=explode("#", $str);
		foreach ($con as $element)
		{
			$task=new TaskModel();
			$task->Unserialize($element);
			array_push($tasks,$task);
		}
		
		return $tasks;
	}
}

Class CategoryModel
{
	public $CategoryID;
	public $Scores;
	public $Difficulty;
	public $Repeation;
	
	public function __construct()
	{
		$this->CategoryID=-1;
		$this->Scores=-1;
		$this->Difficulty=-1;
		$this->Repeation=-1;
	}
	
	public function Serialize()
	{
		$str=$this->CategoryID.','.$this->Scores.','.$this->Difficulty.','.$this->Repeation.';';
	
		return $str;
	}
	
	public function Unserialize($str)
	{
		$con=explode(",", $str);
		if (count($con)!=4) throw new Exception("Invalid Serialize String");
		
		$this->CategoryID=$con[0];
		$this->Scores=$con[1];
		$this->Difficulty=$con[2];
		$this->Repeation=$con[3];
	}		
}

?>